Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Time Data: DATE, TIME, And TIMESTAMP

The DATE, TIME, and TIMESTAMP data types are used to represent dates and times. Data of type DATE supports specification of the month, day, and year, and is represented as a JDBC Date object. Data of type TIME supports specification of the hour, minutes, seconds, and milliseconds, and is represented as a JDBC Time object. Data of type TIMESTAMP supports specification of the month, day, year, hour, minutes, seconds, and milliseconds, and is represented as a JDBC Timestamp object. The Date, Time, and Timestamp objects, which we’ll get into a bit later, are new with JDBC.


Tip:  Be aware of date limitations.
One important note about Date and Timestamp objects: The Java calendar starts at January 1, 1970, which means that you cannot represent dates prior to 1970.

New Data Classes

The JDBC API introduced several new data classes. These classes were developed to solve specific data-representation problems like how to accurately represent fixed-precision numeric values (such as currency values) for NUMERIC and DECIMAL data types, and how to represent time data for DATE, TIME, and TIMESTAMP data types.

Numeric

As mentioned before, the Numeric class was introduced with the JDBC API to represent signed, exact numeric values with a fixed number of decimal places. This class is ideal for representing monetary values, allowing accurate arithmetic operations and comparisons. Another aspect is the ability to change the rounding value. Rounding is performed if the value of the scale (the number of fixed decimal places) plus one digit to the right of the decimal point is greater than the rounding value. By default, the rounding value is 4. For example, if the result of an arithmetic operation is 2.495, and the scale is 2, the number is rounded to 2.50. Listing 10.6 provides an example of changing the rounding value. Imagine that you are a devious retailer investigating ways to maximize your profit by adjusting the rounding value.

Listing 10.6 Changing the rounding value.

import java.sql.*;

class NumericRoundingValueTest {

    public static void main(String args[]) {

        // Set our price and discount amounts
        Numeric price = new Numeric(4.91, 2);
        Numeric discount = new Numeric(0.15, 2);
        Numeric newPrice;

        // Give the item a discount
        newPrice = discountItem(price, discount);

            System.out.println("discounted price="+newPrice.toString());

        // Now, give the item a discount with a higher rounding value.
        // This will lessen the discount amount in many cases.
        discount.setRoundingValue(9);

       newPrice = discountItem(price, discount);

        System.out.println("discounted price with high rounding="+
                newPrice.toString());
    }

    // Perform the calculation to discount a price
    public static Numeric discountItem(
        Numeric price,
        Numeric discount)
    {
        return price.subtract(price.multiply(discount));
    }
}

Listing 10.6 produces the following output:

discounted price=004.17
discounted price with high rounding=004.18

Date

The Date class is used to represent dates in the ANSI SQL format YYYY-MM-DD, where YYYY is a four-digit year, MM is a two-digit month, and DD is a two-digit day. The JDBC Date class extends the existing java.util.Date class (setting the hour, minutes, and seconds to zero) and, most importantly, adds two methods to convert Strings into dates, and vice-versa:

// Create a Date object with a date of June 30th, 1996
Date d = Date.valueOf("1996-06-30");

// Print the date
System.out.println("Date=" + d.toString());

// Same thing, without leading zeros
Date d2 = Date.valueOf("1996-6-30");
System.out.println("Date=" + d2.toString());

The Date class also serves very well in validating date values. If an invalid date string is passed to the valueOf method, a java.lang.IllegalArgument-Exception is thrown:

String s;

// Get the date from the user
.
.
.
//   Validate the date
try  {
      Date d = Date.valueOf(s);
}
catch  (java.lang.IllegalArgumentException ex) {
       // Invalid date, notify the application
    .
    .
    .
}

It is worth mentioning again that the Java date epoch is January 1, 1970; therefore, you cannot represent any date values prior to January 1, 1970, with a Date object.

Time

The Time class is used to represent times in the ANSI SQL format HH:MM:SS, where HH is a two-digit hour, MM is a two-digit minute, and SS is a two-digit second. The JDBC Time class extends the existing java.util.Date class (setting the year, month, and day to zero) and, most importantly, adds two methods to convert Strings into times, and vice-versa:

// Create a Time object with a time of 2:30:08 pm
Time t = Time.valueOf("14:30:08");

// Print the time
System.out.println("Time=" + t.toString());

// Same thing, without leading zeros
Time t2 = Time.valueOf("14:30:8");
System.out.println("Time=" + t2.toString());

The Time class also serves very well in validating time values. If an invalid time string is passed to the valueOf method, a java.lang.IllegalArgument-Exception is thrown:

String s;

// Get the time from the user
.
.
.
// Validate the time
try {
     Time t = Time.valueOf(s);
}
catch  (java.lang.IllegalArgumentException ex) {
       // Invalid time, notify the application
    .
    .
    .
}


Previous Table of Contents Next